(function(w){
    var login = function(){
        this.init();
        return this;
    }, c,userId = getQueryString("userId");

    login.prototype = {
        init: function(){
            this.data = {"url":"/rest/v1.0/backoffice/Systems/getSystemConfig"};
            netWork(this.data,this.getResUrl);

            c = this;
            c.data = {};
            if(sessionStorage.loginData != null && sessionStorage.resUrl != null){
                location.href = "/pages/moderatorManager/html/index.html";
            }
            $("#loginId").val(localStorage.userName?localStorage.userName:"");
        },
        loginCheck: function(d){
            alert( JSON.stringify(d));

            if(d.data && d.data.length > 0){
                alert(11);
                sessionStorage.loginData = JSON.stringify(d);
                localStorage.userName = $("#loginId").val();
                location.href = "./index.html";
            }
        },
        loginHandle: function(data){
            var d = eval("("+data+")");
            if(d.data && d.data.length > 0){
                sessionStorage.loginData = JSON.stringify(d);
                localStorage.userName = $("#loginId").val();
                location.href = "./index.html";
            }else{
                $("#tip").text("用户名或密码错误！");
            }
        },
        getResUrl: function(data){
            var d = eval("("+data+")");
            if(d.result.status == "1")sessionStorage.resUrl = d.data.resUrl;
            $("#submit").trigger("trigger2");
        },
        evt: function(){
            $("#submit").on("click",function(){
                if(trim($("#loginId").val()) == ""){
                    $("#loginId").css("border","1px solid #ffdcb3;");
                    $("#tip").text("请输入用户名");
                    return;
                }
                if(trim($("#password").val()) == ""){
                    $("#password").css("border","1px solid #ffdcb3;");
                    $("#tip").text("请输入密码");
                    return;
                }
                $("#submit").trigger("trigger");
            });

            $("#loginId,#password").on("input",function(){
                $(this).css("border","1px solid #e6eaec");
                $("#tip").text("");
            });

            $("#submit").on("trigger",function(){
                c.data = {"url":"/rest/v1.0/backoffice/adminUser/loginByModerator","method":"post","data": "loginId="+$("#loginId").val()+"&password="+$("#password").val()};
                netWork(c.data,c.loginHandle);
            });
            $("#submit").on("trigger2",function(){
                if(userId != ""){
                    c.data = {"url":"/rest/v1.0/backoffice/adminUser/loginAdminByUserId","method":"post","data": {"userId":userId},"native":false};
                    netWork(c.data,c.loginCheck);
                }
            });
        }
    };
    w.login = login;
}(window));

new login().evt();